<?php

/**
 * @author lzp300
 * @copyright 2015-03-26
 */

class index extends actionAbstract{
    /**
     * 构造函数
     * @var array
     */
    function __construct() {
        parent::__construct();
	    set_time_limit(0);
    }
    
    /**
     * 权限MD5加密
     */
    function SignUtil($serviceName="1",$accountKey="qingmangguokey",$version="1.1.0",$accountID="qingmangguo"){
        $reqTime=date('Y-m-d',time());
        
        $resuoce=array();
        $resuoce['state']=0;
        $resuoce['string']="";
        
        $originalArray=array();
        $originalArray['Version']=$version;
        $originalArray['AccountID']=$accountID;
        $originalArray['ReqTime']=$reqTime;
        if($serviceName){
            $originalArray['ServiceName']=$serviceName;
            ksort($originalArray);
            $sortedArray="";
            foreach($originalArray as $k=>$v){
                $sortedArray.=$k.'='.$v.'&';
            }
            $sortedArray=trim($sortedArray,'&');
            $keywords=md5($sortedArray.$accountKey);
            $resuoce['state']=1;
            $resuoce['string']['version']=$version;
            $resuoce['string']['accountId']=$accountID;
            $resuoce['string']['serviceName']=$serviceName;
            $resuoce['string']['sign']=$keywords;
            $resuoce['string']['reqTime']=$reqTime;
            //var_dump($resuoce);
        }else{
            $resuoce['string']='接口方法名错误！';
        }
        return $resuoce;
    }
	
    /**
     * curl 传输
     */
    function curl($post_data,$url="http://120.24.75.164:8999/OtaInterfaceConnections/OtaDataInterFace/getOtaMethod"){
        //print_r($post_data);exit;
        $ch = curl_init();
        //print_r($ch);
        curl_setopt ( $ch, CURLOPT_URL, $url );
        curl_setopt ( $ch, CURLOPT_POST, 1 );
        curl_setopt ( $ch, CURLOPT_HEADER, 0 );
        curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
        curl_setopt ( $ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json; charset=utf-8"));
        curl_setopt ( $ch, CURLOPT_POSTFIELDS, $post_data );
        $return = curl_exec ( $ch );
        curl_close ( $ch );
		
		$logs="调用地址：".$url."\r\n接收数据：".$return."\r\n发送数据：".$post_data;
        $this->setLogs($logs,"/data/Logs/index/");
		
        $return=json_decode($return,true);
        if($return){
            $body=isset($return['body'])?$return['body']:'';
            $head=isset($return['head'])?$return['head']:'';
			if($head&&is_array($head)&&$head['status']==0&&$head['responseCode']==0000){
				return $body;
			}else{
				return "数据错误，请联系技术人员！";
			}
        }
        return "获取内容失败！";
        //echo "<pre>";print_r($head);exit;
    }
    /**
     * 公共调用
     */
    function thall($actionName,$body,$url="http://120.24.75.164:8999/OtaInterfaceConnections/OtaDataInterFace/getOtaMethod"){
    	$resuoce=$this->SignUtil($actionName);
    	if($resuoce['state']==1){
    		$post_data=json_encode(array("head"=>$resuoce['string'],"body"=>$body));
			$contArray=$this->curl($post_data,$url);
            return $contArray;
    	}else{
    		return $resuoce['string'];
    	}
    }
	
	
	//获取渠道
    function GetOsii_channels(){
        $resuoce=$this->SignUtil('GetAllOtaPartnerInfo','osii_interface_2004_!@#','1.1.0','osii_interface_2004');
        if($resuoce['state']==0){
            exit(json_encode(array('state'=>0,'html'=>$resuoce['string'])));
        }elseif($resuoce['state']==1){
            $post_data=json_encode(array("head"=>$resuoce['string'],"body"=>array()));
            $contArray=$this->curl($post_data,"http://www.htapi.com/OsiiInterFace/getMethod.do");
            $this->loadModel('qm','channels');
			$j=0;
            if($contArray&&is_array($contArray)){
                foreach($contArray as $k=>$v){
                    $data['osiiid']=$v['otaCode'];
                    $data['osiitype']=$v['otaType'];
                    $data['title']=$v['otaName'];
					$data['status']=$v['otaStatus'];
					$data['created']=date('Y-m-d H:i:s',time());
                    $re=$this->qm->channelsModel->fetchRow("select id from qm_channels where osiiid=".$v['otaCode']);
                    if($re){$retu=$this->qm->channelsModel->update($data,$re['id']);}
					else{$retu=$this->qm->channelsModel->insert($data);}
					if($retu){$j++;}
                }
            }
			exit(json_encode(array('state'=>1,'html'=>"成功更新 ".$j." 个渠道！")));
        }
    }
	//------------------
	function insertID(){
		$this->loadModel('qm','hotel');
		$this->loadModel('qm','uphotel');//阿里
		$this->loadModel('xc','uphotel');//携程
		$this->loadModel('kall','uphotel');
		$this->loadApi('orderapi');
		$orderapi = new orderapi();
		/*
		//获取可售更新到数据库
		$GetImmediateHotelList=$orderapi->GetImmediateHotelListTO();
		foreach($GetImmediateHotelList as $k=>$v){
			$data['hid']=$v;$data['status']=1;$data['created']=date('Y-m-d H:i:s',time());
			$this->kall->uphotelModel->insert($data);
		}
		*/
		
		/*
		//取出最新的酒店上架
		$string="";
		$re=$this->kall->uphotelModel->fetchAll("select hid from kall_uphotel");
		foreach($re as $k=>$v){
			$as=$this->qm->hotelModel->fetchRow("select id from qm_hotel where id>=3074 and hid='".$v['hid']."'");
			if(!$as){$string.=",".$v['hid'];$data['hid']=$v['hid'];$this->qm->hotelModel->insert($data);}
		}
		echo $string;exit;
		*/
		
		/*
		//阿里上架酒店更新
		$string="178680,178683,178705,175347,128015,175320,178124,178657,178699,6021,175325,1019,88951,178655,63456,177786,177618,178646,178953,34071,32055,178673,178103,88651,175285,178959,75151,178960,15496,178659,178669,178660,178661,7518,12817,167328,178695,20217,178670,178651,170910,175304,54134,175302,178134,178113,175364,151650,872,178115,178712,178848,175342,178710";
		$stringArr=explode(',',$string);
		foreach($stringArr as $k=>$v){
			$data['hid']=$v;$data['status']=1;$data['created']=date('Y-m-d H:i:s',time());
			$this->qm->uphotelModel->insert($data);
		}
		*/
		
		/*
		//携程上架酒店更新
		$string="178683,175347,175320,178124,178699,175325,178655,177618,178646,34071,178673,175285,15496,178669,7518,12817,167328,20217,178670,178651,170910,54134,178134,178113,175364,872,178115,178848,175342,178710";
		$stringArr=explode(',',$string);
		foreach($stringArr as $k=>$v){
			$data['hid']=$v;$data['status']=1;$data['created']=date('Y-m-d H:i:s',time());
			$this->xc->uphotelModel->insert($data);
		}
		*/
	}
	
	
	
	
	/*
    //获取省
    function GetOsii_Province(){
        $resuoce=$this->SignUtil('GetOsii_Province');
        if($resuoce['state']==0){
            echo $resuoce['string'];
        }elseif($resuoce['state']==1){
            $post_data=json_encode(array("head"=>$resuoce['string'],"body"=>array()));
            $contArray=$this->curl($post_data);
            
            $this->loadModel('tc','province');
            if($contArray&&is_array($contArray)){
                foreach($contArray as $k=>$v){
                    $data['name']=$v['name'];
                    $data['en_name']=$v['en_name'];
                    $data['prefixLetter']=$v['prefix_letter'];
                    $re=$this->tc->provinceModel->fetchRow("select * from tc_province where osiiid=".$v['id']);
                    if($re){
                        $retu=$this->tc->provinceModel->update($data,$re['id']);
                    }else{
                        $data['osiiid']=$v['id'];
                        $retu=$this->tc->provinceModel->insert($data);
                    }
                }
            }
            if($retu){
                echo "OK";
            }else{
                echo "NO";
            }
            //echo '<pre>';print_r($contArray);
        }
    }
    
    //获取市
    function GetOsii_City(){
        $resuoce=$this->SignUtil('GetOsii_City');
        if($resuoce['state']==0){
            echo $resuoce['string'];
        }elseif($resuoce['state']==1){
            $post_data=json_encode(array("head"=>$resuoce['string'],"body"=>array()));
            $contArray=$this->curl($post_data);
            
            $this->loadModel('tc','city');
            if($contArray&&is_array($contArray)){
                foreach($contArray as $k=>$v){
                    sleep(2);
                    $data['name']=$v['name'];
                    $data['en_name']=$v['en_name'];
                    $data['prefix_letter']=$v['prefix_letter'];
                    $data['province_id']=$v['province_id'];
                    $re=$this->tc->cityModel->fetchRow("select * from tc_city where osiiid=".$v['id']);
                    if($re){
                        $retu=$this->tc->cityModel->update($data,$re['id']);
                    }else{
                        $data['osiiid']=$v['id'];
                        $retu=$this->tc->cityModel->insert($data);
                    }
                    ob_flush();flush();
                }
            }
            if($retu){
                echo "OK";
            }else{
                echo "NO";
            }
            //echo '<pre>';print_r($contArray);
        }
    }
    
    //获取区
    function GetOsii_County(){
        $resuoce=$this->SignUtil('GetOsii_County');
        if($resuoce['state']==0){
            echo $resuoce['string'];
        }elseif($resuoce['state']==1){
            $post_data=json_encode(array("head"=>$resuoce['string'],"body"=>array()));
            $contArray=$this->curl($post_data);
            
            $this->loadModel('tc','county');
            if($contArray&&is_array($contArray)){
                foreach($contArray as $k=>$v){
                    sleep(2);
                    $data['name']=$v['name'];
                    $data['en_name']=$v['en_name'];
                    $data['prefix_letter']=$v['prefix_letter'];
                    $data['city_id']=$v['city_id'];
                    $re=$this->tc->countyModel->fetchRow("select * from tc_city where osiiid=".$v['id']);
                    if($re){
                        $retu=$this->tc->countyModel->update($data,$re['id']);
                    }else{
                        $data['osiiid']=$v['id'];
                        $retu=$this->tc->countyModel->insert($data);
                    }
                    ob_flush();flush();
                }
            }
            if($retu){
                echo "OK";
            }else{
                echo "NO";
            }
            //echo '<pre>';print_r($contArray);
        }
    }
    
	//获取酒店信息
    function GetHotelInfo(){
		$city = isset($_POST['city'])?(int)($_POST['city']):0;   
		$country = isset($_POST['county'])?(int)($_POST['county']):0;
  		$this->loadModel('tc','city');
		$where="";
		if($city){
			$where.=" where a.osiiid='".$country."'";
			if($country){$where.=" and b.osiiid='".$country."'";}
		}
        
		$infosql="select a.osiiid as city_osiiid,b.osiiid as county_osiiid from tc_city as a inner join tc_county as b on a.osiiid=b.city_id".$where;
        $arr=$this->tc->cityModel->fetchAll($infosql);
        $body=array();$areOk=0;
		if($arr){
			foreach ($arr as $key=>$value) {
				$body["cityCode"]=$value['city_osiiid'];
				$body["countyCode"]=$value['county_osiiid'];
				$return=$this->thall("GetHotelInfo",$body);
				if($return&&is_array($return)){
					$rst=$this->tenHotel($return);
					$areOk+=$rst;
				}
			}
		}
        if($areOk>0){exit(json_encode(array('state'=>1,'html'=>"更新成功，获取了 ".$areOk." 家酒店！")));}
		else{exit(json_encode(array('state'=>0,'html'=>"更新失败，无更新酒店！")));}
	}
	function tenHotel($resuoce=array()){
        $this->loadModel('hotel','category');
		$areOk=0;
		foreach($resuoce as $k=>$v){
			$isset=$this->hotel->categoryModel->fetchRow("select id from hotel_category where hotelId='".$v['hotelCode']."'");
			$data['hotelId']=$v['hotelCode'];
			$data['hotelName']=empty($v['hotelName'])?(isset($v['hotelEnName'])?'':$v['hotelEnName']):$v['hotelName'];
			$data['tel']=isset($v['telephone'])?$v['telephone']:'';
			$data['province']=$v['provinceCode'];
			$data['city']=$v['cityCode'];
			$data['county']=$v['countyCode'];
			$data['address']=isset($v['address'])?$v['address']:'';
			$data['star']=isset($v['star'])?$v['star']:0;
			$data['introduce']=isset($v['brief'])?$v['brief']:'';
			$data['description']=isset($v['hotelDescription'])?$v['hotelDescription']:'';
			$data['created']=date('Y-m-d H:i:s',time());
			if($isset){
				$re=$this->hotel->categoryModel->update($data,$isset['id']);
			}
			else{$re=$this->hotel->categoryModel->insert($data);}
			if($re){$areOk+=1;}
		}
		return $areOk;
	}
    
    //获取房型
    function GetRoomTypeInfo(){
		$category=isset($_POST['category'])?(int)($_POST['category']):0;
		$city=isset($_POST['city'])?(int)($_POST['city']):0;
        $this->loadModel('hotel','category');
		$where="";
		if($category){$where=" where hotelId='".$category."'";}
		elseif($city){$where=" where city='".$city."'";}
		
        $re=$this->hotel->categoryModel->fetchAll("select hotelId from hotel_category".$where);
		$body=array();$areOk=0;
        if($re){
			foreach($re as $k=>$v){
				$body['hotelCode']=$v['hotelId'];
				$return=$this->thall("GetRoomTypeInfo",$body);
				if($return&&is_array($return)){
					$rst=$this->tenType($return);
					$areOk+=$rst;
				}
			}
        }
		if($areOk>0){exit(json_encode(array('state'=>1,'html'=>"更新成功，获取了 ".$areOk." 个房型！")));}
		exit(json_encode(array('state'=>0,'html'=>"更新失败，无更新房型！")));
    }
	function tenType($resuoce=array()){
        $this->loadModel('hotel','type');
		$areOk=0;
		if($resuoce){
			foreach($resuoce as $k=>$v){
				$isset=$this->hotel->typeModel->fetchRow("select id from hotel_type where hotelId='".$v['hotelCode']."' and typeId='".$v['roomTypeCode']."'");
				$data['hotelId']=$v['hotelCode'];
				$data['typeId']=$v['roomTypeCode'];
				$data['title']=$v['roomName'];
				$data['bed']=isset($v['bedTypeName'])?$v['bedTypeName']:'';
				$data['maxnumber']=isset($v['person'])?$v['person']:0;
				$data['floor']=isset($v['floorRange'])?$v['floorRange']:'';
				$data['area']=isset($v['areaRange'])?$v['areaRange']:0;
				$data['created']=date('Y-m-d H:i:s',time());
				if($isset){
					$atr=$this->hotel->typeModel->update($data,$isset['id']);
				}
				else{$atr=$this->hotel->typeModel->insert($data);}
				if($atr){$areOk+=1;}
			}
		}
		return $areOk;
	}
	
	//获取图片
    function GetHotelImageInfo(){
		$category=isset($_POST['category'])?(int)($_POST['category']):0;
		$city=isset($_POST['city'])?(int)($_POST['city']):0;
        $this->loadModel('hotel','category');
        $this->loadModel('hotel','picture');
		$where="";
		if($category){$where=" where hotelId='".$category."'";}
		elseif($city){$where=" where city='".$city."'";}
		
        $re=$this->hotel->categoryModel->fetchAll("select hotelId from hotel_category".$where);
        $body=array();$areOk=0;
        if($re){
			foreach($re as $k=>$v){
				$body['hotelCode']=$v['hotelId'];
				$return=$this->thall("GetHotelImageInfo",$body);
				if($return&&is_array($return)){
					$this->hotel->pictureModel->delete("hotelId='".$v['hotelId']."'");
					$rst=$this->tenPicture($return);
					$areOk+=$rst;
				}
			}
        }
        if($areOk>0){exit(json_encode(array('state'=>1,'html'=>"更新成功，获取了 ".$areOk." 个图片！")));}
		else{exit(json_encode(array('state'=>0,'html'=>"更新失败，无更新房型！")));}
    }
	function tenPicture($resuoce=array()){
        $this->loadModel('hotel','picture');
		$areOk=0;
		foreach($resuoce as $k=>$v){
			$data['hotelId'] = $v['hotelCode'];
			$data['title'] = $v['picName'];
			$data['pcurl'] = $v['picUrl'];
			$data['status'] = $v['licensedDownload'];
			$data['created']=date('Y-m-d H:i:s',time());
			$atr=$this->hotel->pictureModel->insert($data);
			if($atr){$areOk+=1;}
		}
		return $areOk;
	}
	
    //获取设备
    function GetHotelFacilityInfo(){
		$category=isset($_POST['category'])?(int)($_POST['category']):0;
		$city=isset($_POST['city'])?(int)($_POST['city']):0;
        $this->loadModel('hotel','category');
		$where="";
		if($category){$where=" where hotelId='".$category."'";}
		elseif($city){$where=" where city='".$city."'";}
        
        $re=$this->hotel->categoryModel->fetchAll("select hotelId from hotel_category".$where);
        $body=array();$areOk=0;
        if($re){
			foreach($re as $k=>$v){
				$body[$k]['hotelCode']=$v['hotelId'];
				$return=$this->thall("GetHotelFacilityInfo",$body);
				if($return&&is_array($return)){
					$rst=$this->tenDevices($return);
					$areOk+=$rst;
				}
			}
        }
        if($areOk>0){exit(json_encode(array('state'=>1,'html'=>"更新成功，获取了 ".$areOk." 个设施设备！")));}
		else{exit(json_encode(array('state'=>0,'html'=>"更新失败，无更新房型！")));}
    }
	function tenDevices($resuoce=array()){
		$this->loadModel('hotel','devices');
		$areOk=0;
		foreach($resuoce as $k=>$v){
			$data['hotelId']=$v['hotelCode'];
			$data['facilityId']=$v['facilityId'];
			$data['title']=$v['facilityName'];
			$data['describe']=isset($v['facilityDescript'])?$v['facilityDescript']:'';
			$data['created']=date('Y-m-d H:i:s',time());
			if($isset){
				$atr=$this->hotel->devicesModel->update($data,$isset['id']);
			}else{
				$atr=$this->hotel->devicesModel->insert($data);
			}
			if($atr){$areOk+=1;}
		}
		return $areOk;
	}
    
    
    
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	//根据pmsId  获取信息
    function getHotelInfoByPmsId(){
		$this->loadModel('hotel','category');
		$body['pmsId']=isset($_GET['pmsId'])?$_GET['pmsId']:'';
		$body['page']=isset($_GET['page'])?(int)($_GET['page']):0;
		if(!$body['pmsId']||!$body['page']){echo "参数错误！"; exit;}
		$arrs=$this->thall("getHotelInfoByPmsId",$body);
		
		if(!is_array($arrs)||!isset($arrs['list'])||!is_array($arrs['list'])){echo "获取内容失败！"; exit;}
		else{
			$areOk=0;
			foreach($arrs['list'] as $k=>$v){
				if(is_object($v)){$arrs['list'][$k]=(array)$v;}
			}
			foreach($arrs['list'] as $ka=>$va){
				$isset=$this->hotel->categoryModel->fetchRow("select id from hotel_category where hotelId='".$va['poid']."'");
				$data['hotelId']=isset($va['poid'])?$va['poid']:'';
				$data['hotelName']=empty($va['hotelName'])?(isset($va['hotelEnName'])?'':$va['hotelEnName']):$va['hotelName'];
				$data['tel']=isset($va['telephone'])?$va['telephone']:'';
				$data['province']=isset($va['provinceCode'])?$va['provinceCode']:'';
				$data['city']=isset($va['cityCode'])?$va['cityCode']:'';
				$data['county']=isset($va['countyCode'])?$va['countyCode']:'';
				$data['address']=isset($va['address'])?$va['address']:'';
				$data['star']=isset($va['star'])?$va['star']:0;
				$data['introduce']=isset($va['brief'])?$va['brief']:'';
				$data['description']=isset($va['hotelDescription'])?$va['hotelDescription']:'';
				if($isset){
					$data['created']=date('Y-m-d H:i:s',time());
					$re=$this->hotel->categoryModel->update($data,$isset['id']);
				}else{
					$re=$this->hotel->categoryModel->insert($data);
				}
				if($re){
					$areOk+=1;
				}
			}
			echo "总共".count($arrs['list'])."条\r\n更新".$areOk."条\r\n共".isset($arrs['total'])?$arrs['total']:'0'."页\r\n当前第".$body['page']."页";
		}
	}
    function getRoomTypeByPmsId(){
		$this->loadModel('hotel','type');
		$body['pmsId']=isset($_GET['pmsId'])?$_GET['pmsId']:'';
		$body['page']=isset($_GET['page'])?(int)($_GET['page']):0;
		if(!$body['pmsId']||!$body['page']){echo "参数错误！"; exit;}
		$arrs=$this->thall("getRoomTypeByPmsId",$body);
		
		if(!is_array($arrs)){echo "获取内容失败！"; exit;}
		if(!is_array($arrs['list'])){echo "获取内容失败！"; exit;}
		$areOk=0;
		foreach($arrs['list'] as $key=>$val){
			$isset=$this->hotel->typeModel->fetchRow("select id from hotel_type where hotelId='".$val['poId']."' and typeId='".$val['roomTypeCode']."'");
			$data['hotelId']=isset($val['poId'])?$val['poId']:'';
			$data['typeId']=isset($val['roomTypeCode'])?$val['roomTypeCode']:'';
			$data['title']=isset($val['roomName'])?$val['roomName']:'';
			$data['bed']=isset($val['bedTypeName'])?$val['bedTypeName']:'';
			$data['maxnumber']=isset($val['person'])?$val['person']:0;
			$data['floor']=isset($val['floorRange'])?$val['floorRange']:'';
			$data['area']=isset($val['areaRange'])?$val['areaRange']:0;        
			if($isset){
				$data['created']=date('Y-m-d H:i:s',time());
				$atr=$this->hotel->typeModel->update($data,$isset['id']);
			}else{
				$atr=$this->hotel->typeModel->insert($data);
			}
			if($atr){
				$areOk+=1;
			}
		}
		echo "总共".count($arrs)."条\r\n更新".$areOk."条\r\n共".isset($arrs['total'])?$arrs['total']:'0'."页\r\n当前第".$body['page']."页";
	}
	function getHotelImageByPmsId(){
		$this->loadModel('hotel','picture');
		$body['pmsId']=isset($_GET['pmsId'])?$_GET['pmsId']:'';
		$body['page']=isset($_GET['page'])?(int)($_GET['page']):0;
		if(!$body['pmsId']||!$body['page']){echo "参数错误！"; exit;}
		$arrs=$this->thall("getHotelImageByPmsId",$body);
		
		if(!is_array($arrs)){echo "获取内容失败！"; exit;}
		if(!is_array($arrs['list'])){echo "获取内容失败！"; exit;}
		$areOk=0;
		foreach($arrs as $k=>$v){
			$re=$this->hotel->pictureModel->delete("hotelId='".$v['poId']."'");
		}
		foreach($arrs['list'] as $k=>$v){
			$isset=$this->hotel->pictureModel->fetchRow("select id from hotel_picture where hotelId='".$v['poId']."'");
			$data['hotelId'] = isset($v['poId'])?$v['poId']:'';
			$data['title'] = isset($v['picName'])?$v['picName']:'';
			$data['pcurl'] = isset($v['picUrl'])?$v['picUrl']:'';
			$data['status'] = isset($v['licensedDownload'])?$v['licensedDownload']:'';
			$atr=$this->hotel->pictureModel->insert($data);
			if($atr){
				$areOk+=1;
			}
		}
		echo "总共".count($arrs)."条\r\n更新".$areOk."条\r\n共".isset($arrs['total'])?$arrs['total']:'0'."页\r\n当前第".$body['page']."页";
	}
    */
}